﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Generator.CodeGeneratorPaddingHelper
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;

namespace System.Web.Razor.Generator
{
  internal static class CodeGeneratorPaddingHelper
  {
    private static readonly char[] _newLineChars = new char[2]
    {
      '\r',
      '\n'
    };

    public static int PaddingCharCount(RazorEngineHost host, Span target, int generatedStart)
    {
      int padding = CodeGeneratorPaddingHelper.CalculatePadding(host, target, generatedStart);
      int result;
      return host.DesignTimeMode && host.IsIndentingWithTabs ? Math.DivRem(padding, host.TabSize, out result) + result : padding;
    }

    public static string PadStatement(
      RazorEngineHost host,
      string code,
      Span target,
      ref int startGeneratedCode,
      out int paddingCharCount)
    {
      if (host == null)
        throw new ArgumentNullException(nameof (host));
      int padding = target != null ? CodeGeneratorPaddingHelper.CalculatePadding(host, target, 0) : throw new ArgumentNullException(nameof (target));
      if (host.DesignTimeMode && padding > 0 && (target.Previous.Kind == SpanKind.Transition && string.Equals(target.Previous.Content, SyntaxConstants.TransitionString)))
      {
        --padding;
        --startGeneratedCode;
      }
      return CodeGeneratorPaddingHelper.PadInternal(host, code, padding, out paddingCharCount);
    }

    public static string Pad(
      RazorEngineHost host,
      string code,
      Span target,
      out int paddingCharCount)
    {
      int padding = CodeGeneratorPaddingHelper.CalculatePadding(host, target, 0);
      return CodeGeneratorPaddingHelper.PadInternal(host, code, padding, out paddingCharCount);
    }

    public static string Pad(
      RazorEngineHost host,
      string code,
      Span target,
      int generatedStart,
      out int paddingCharCount)
    {
      int padding = CodeGeneratorPaddingHelper.CalculatePadding(host, target, generatedStart);
      return CodeGeneratorPaddingHelper.PadInternal(host, code, padding, out paddingCharCount);
    }

    internal static int CalculatePadding(RazorEngineHost host, Span target, int generatedStart)
    {
      if (host == null)
        throw new ArgumentNullException(nameof (host));
      if (target == null)
        throw new ArgumentNullException(nameof (target));
      int num = CodeGeneratorPaddingHelper.CollectSpacesAndTabs(target, host.TabSize) - generatedStart;
      if (num < 0)
        num = 0;
      return num;
    }

    private static string PadInternal(
      RazorEngineHost host,
      string code,
      int padding,
      out int paddingCharCount)
    {
      if (host.DesignTimeMode && host.IsIndentingWithTabs)
      {
        int result;
        int count = Math.DivRem(padding, host.TabSize, out result);
        paddingCharCount = count + result;
        return new string('\t', count) + new string(' ', result) + code;
      }
      paddingCharCount = padding;
      return code.PadLeft(padding + code.Length, ' ');
    }

    private static int CollectSpacesAndTabs(Span target, int tabSize)
    {
      Span span1 = target;
      string str1 = (string) null;
      for (; span1.Previous != null; span1 = span1.Previous)
      {
        string str2 = span1.Previous.Content ?? string.Empty;
        int num = str2.LastIndexOfAny(CodeGeneratorPaddingHelper._newLineChars);
        if (num >= 0)
        {
          if (num != str2.Length - 1)
          {
            span1 = span1.Previous;
            str1 = str2.Substring(num + 1);
            break;
          }
          break;
        }
      }
      Span span2 = span1;
      if (str1 == null)
        str1 = span2.Content;
      int num1 = 0;
      while (span2 != target)
      {
        if (str1 != null)
        {
          for (int index = 0; index < str1.Length; ++index)
          {
            if (str1[index] == '\t')
              num1 += tabSize - num1 % tabSize;
            else
              ++num1;
          }
        }
        span2 = span2.Next;
        str1 = span2.Content;
      }
      return num1;
    }
  }
}
